package JavaSpark;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

public class JavaPersist {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf()
                .setAppName("JavaWordCount")
                .setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
        //JavaRDD<String> lines = sc.textFile("C://Users//10902//Desktop//spark.txt");
        //cache()或者persist()的使用
        //必须在transformation或textFile等创建了一个RDD之后，直接连续调用cache()或persist()才可以
        //如果你先创建一个RDD，然后单独另起一行执行cache()或persist()方法，是没有用的
        //并且，会报错，大量为文件会丢失
        JavaRDD<String> lines = sc.textFile("C://Users//10902//Desktop//spark.txt").cache();
        long beginTime = System.currentTimeMillis();
        long count = lines.count();
        System.out.println(count);
        long endTime = System.currentTimeMillis();
        System.out.println("COST ：" + (endTime - beginTime) + "milliseconds");

        beginTime = System.currentTimeMillis();
        count = lines.count();
        System.out.println(count);
        endTime = System.currentTimeMillis();
        System.out.println("COST ：" + (endTime - beginTime) + "milliseconds");

        sc.close();
    }
}
